import { Service } from '../../services/service'
import { formaturl } from '@/utils/commonUtils'
import dayjs from 'dayjs'
const state = {
  pagnation: {
    pageSize: 10,
    pageNum: 1,
    total: 0
  },
  reqUrlMapping: {
    1: '/hanyun/app-service/api/v1/tbShopAppSubscribe/query',
    2: '/hanyun/app-service/api/v1/tbShopServiceSubscribe/query',
    3: '/hanyun/app-service/api/v1/tbShopModelSubscribe/query'
  },
  tableTab: '1',
  pageData: [],
  currentTabBackup: ''
}
const getters = {}
const mutations = {
  setPageData(state, data) {
    state.pageData = data
  },
  setPagnation(state, data) {
    state.pagnation = Object.assign({}, state.pagnation, data)
  },
  setTableTab(state, data) {
    state.tableTab = data
  },
  setCurrentTabBackup(state, data) {
    state.currentTabBackup = data
    sessionStorage.setItem('currentTabBackup', data)
  }
}
const actions = {
  async getList({ state, commit }) {
    const retData = await Service({
      url: state.reqUrlMapping[state.tableTab] + formaturl(state.pagnation),
      method: 'get'
    })
    if (retData.status === 200) {
      const data = retData.result.data
      const result = retData.result
      data.forEach((e) => {
        e.createDate = dayjs(e.createDate).format('YYYY-MM-DD HH:mm:ss')
      })
      commit('setPageData', data)
      commit('setPagnation', {
        pageSize: result.pageSize,
        pageNum: result.pageNum,
        total: +result.total
      })
    }
  }
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
